package com.example.util.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class CtRedisAutoConfig {

    @Bean
    @DependsOn(value = "ctRedisProperties")
    public RedisTemplate ctRedisTemplate(CtRedisProperties redisProperties) {
        if (redisProperties == null || redisProperties.getHost() == null) {
            return null;
        }
        RedisTemplate template = new RedisTemplate();
        //处理JAVA数据序列化
        //template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        JedisConnectionFactory conn = new JedisConnectionFactory();
        conn.setHostName(redisProperties.getHost());
        conn.setPort(redisProperties.getPort());
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
     /*   jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
        jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
        jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
        jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());*/
        conn.setPoolConfig(jedisPoolConfig);
        conn.setDatabase(redisProperties.getDatabase());
        conn.setPassword(redisProperties.getPassword());
        conn.setTimeout(redisProperties.getTimeout());
        conn.afterPropertiesSet();
        template.setConnectionFactory(conn);
        return template;
    }

   /* @Bean
    @DependsOn(value = "ctRedisTemplate")
    public CtRedisUtil ctRedisUtil(RedisTemplate ctRedisTemplate) {
        CtRedisUtil redisUtil = new CtRedisUtil();
       // redisUtil.setRedisTemplate(ctRedisTemplate);
        return redisUtil;
    }*/
}
